package org.example.controller;

import org.example.mapper.CatalogMapper;
import org.example.pojo.Catalog;
import org.example.pojo.TreeCatalogs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController 
{
	@Autowired
	CatalogMapper catalogMapper;
	
	@GetMapping("getTreeCatalogs")
	@ResponseBody
	public List<TreeCatalogs> getTreeCatalogs()
	{
		List<Catalog> catalogs = catalogMapper.getCatalogs();

		//创建主目录
		List<TreeCatalogs> catalogsList = new ArrayList<>();
		
		//遍历目录
		for(Catalog c : catalogs)
		{
			//转换数据
			TreeCatalogs catalogs1 = new TreeCatalogs(c);
			
			//调用递归方法，进行插入数据
			TreeCatalogs.arrange(c.getParentId(),catalogsList,catalogs1);
		}
		
		return catalogsList;
	}

	@GetMapping("getCatalog")
	@ResponseBody
	public List<Catalog> getCatalog()
	{
		return catalogMapper.getCatalogs();
	}
	
	@GetMapping("getCatalogs")
	@ResponseBody
	public TreeCatalogs getCatalogs()
	{
		return new TreeCatalogs();
	}
	
}
